package com.gusha.controller;

import java.math.BigInteger;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gusha.config.SysOperateLog;
import com.gusha.utils.Constants;
import com.gusha.utils.JsonResult;
import com.gusha.utils.WebValueUtil;

import com.gusha.service.SysMenuService;
import com.gusha.dao.SysMenuDao;
import com.gusha.model.SysMenu;


/**
 * @Created：2018-10-26 15:18:16
 * @Author LightSound的代码生成器
 * @Description:SysMenuController控制类
 */
@Controller
@RequestMapping("/gx/sysMenu")
public class SysMenuController {

    // 注入Dao
    @Autowired
    private SysMenuDao sysMenuDao;
    
    // 注入Service
    @Autowired
    private SysMenuService sysMenuService;

    // 查询列表
    @RequestMapping("list")
    @SysOperateLog(operate = "查看菜单列表")
    public String list(Model model, HttpServletRequest request) {

        //请求参数
        String pageIndex = WebValueUtil.getWebValue(request, "pageIndex");
        String menuName = WebValueUtil.getWebValue(request, "menuName");

        //参数封装
        int pageNo = 0;
        if (StringUtils.isNotBlank(pageIndex)) {
            pageNo = Integer.parseInt(pageIndex);
        }
        SysMenu sysMenu = new SysMenu();
        if (StringUtils.isNotBlank(menuName)) {
        	sysMenu.setMenuName(menuName);
        }

        //获取分页数据
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(pageNo, Constants.DEFAULT_PAGESIZE, sort);
        Page<SysMenu> page = sysMenuService.findAll(pageable, sysMenu);

        model.addAttribute("page", page);
        model.addAttribute("menuName", menuName);
        return "/admin/sysMenu/list";
    }

    // 进入添加页面
    @RequestMapping("toAdd")
    @SysOperateLog(operate = "进入菜单添加页面")
    public String toAdd() {
        return "/admin/sysMenu/form";
    }

    // 进入编辑页面
    @RequestMapping("toEdit")
    @SysOperateLog(operate = "进入菜单编辑页面")
    public String toEdit(Model model, HttpServletRequest request) {

        //请求参数
        String id = WebValueUtil.getWebValue(request, "id");

        if (StringUtils.isNotBlank(id)) {
            if(sysMenuDao.existsById(new BigInteger(id))) {
                SysMenu sysMenu = sysMenuDao.getOne(new BigInteger(id));
                model.addAttribute("obj", sysMenu);
                Integer layerIndex = sysMenu.getMenuLevel()-1;
    			List<SysMenu> listParent = sysMenuDao.findAllByMenuLevelOrderBySortDesc(layerIndex);
    			model.addAttribute("listParent", listParent);
            }
        }        

        return "/admin/sysMenu/form";
    }

    // 保存
    @RequestMapping("save")
    @ResponseBody
    @SysOperateLog(operate = "保存菜单信息")
    public JsonResult save(SysMenu sysMenu) {
        JsonResult result = new JsonResult(false,"保存失败");
        sysMenuDao.saveAndFlush(sysMenu);
        result.setFlag(true);
        result.setMsg("保存成功");

        return result;
    }


    // 删除
    @RequestMapping("delete")
    @ResponseBody
    @SysOperateLog(operate = "删除菜单信息")
    public JsonResult delete(BigInteger id) {
    	JsonResult result = sysMenuService.deleteMenu(id);
		return result;
    }

}
